package com.bootdo.common.utils.image;

import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author guoyunfei on 2021/7/25 18:22
 * @className CompressImg
 * @description TODO
 * @Version 1.0
 */
public class CompressImg {

    private static final String ROOT_PATH = "D:\\Download\\";

    private static final String SAVE_PATH = "D:\\Download\\compress\\";


    public static List<String> imageFormat(){
        List<String> list = new ArrayList<>();
        list.add("jpg");
        list.add("png");
        list.add("jpeg");
        list.add("bpm");
        return list;
    }

    public static List<String> getFilePaths(){

        List<String> list = new ArrayList<>();

        List<String> strings = imageFormat();

        File file = new File(ROOT_PATH);
        File [] files = file.listFiles();

        for (int i = 0; i < files.length; i++){
            File file1 = files[i];

            String extName = FileUtil.extName(file1.getName());

            if(strings.contains(extName)){
                list.add(ROOT_PATH +file1.getName());
            }
        }

        return list;
    }


    public static void main(String[] args) throws IOException {

        File targetFile = new File(SAVE_PATH);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }

        List<String> filePaths = getFilePaths();
        for (int i = 0; i < filePaths.size(); i++) {
            String substring = filePaths.get(i).substring(filePaths.get(i).lastIndexOf("."));
            ImgUtils.compressPic(filePaths.get(i),SAVE_PATH + i + substring);
        }
    }

}
